<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_pembayaran_pinjaman extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_pembayaran_pinjaman_model', 'PinjamanAnggota');
		$this->load->model('combo_model', 'Combo');
		$this->load->model('keu_store_procedure_model', 'SP');
		$this->load->model('keu_coa_model', 'Coa');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Penerimaan Pembayaran Pinjaman Anggota";
		$data['data'] = null;
		$data['data']['date_from'] = date('01/m/Y');
		$data['data']['date_to'] = date('t/m/Y');
		
		viewByTemplate('keu_pembayaran_pinjaman/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->PinjamanAnggota->GetJSON();
	}
	
		
	
	function edit()
	{
		$data['title'] = "Penerimaan Pembayaran Pinjaman Anggota";
		$data['data'] = $this->PinjamanAnggota->GetSingleView(get('id_pinjaman'), get('periode'));
				
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		$data['cost_center_data']	= json_encode($this->Combo->SWCostCenter());
		
		
		
		$data['mode'] = "edit";
		viewByTemplate('keu_pembayaran_pinjaman/form', $data, 'form');
	}

	
	function GetKurs(){
		$arrkurs = $this->PinjamanAnggota->GetKurs(post('kd_valuta'), date_format3(post('tgl_pembayaran_pinjaman'), 'YYYY-MM-DD'));
		echo $arrkurs['harga_rata'];
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		// Validation
		if (post("kd_kantor") == '') {
			$error->Add("kd_kantor", "Kantor", $tpl_empty);
		}
		
		if (post("tgl_transaksi") == '') {
			$error->Add("tgl_transaksi", "Tanggal Dana", $tpl_empty);
		}
		
		if (post("kd_valuta") == '') {
			$error->Add("kd_valuta", "Mata Uang", $tpl_empty);
		}
		
		if (toNumber(post("jml_dana")) == '0') {
			$error->Add("jml_dana", "Jumlah Dana", $tpl_empty);
		}
		
		if (post("keterangan") == '') {
			$error->Add("keterangan", "Keterangan", $tpl_empty);
		}
		
		return $error;
	}
	
	function dtl_account_data()
	{
		echo $this->PinjamanAnggotaDetail->GetJSON();
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kd_kantor', 'tgl_transaksi',  'keterangan',  'kurs', 'kd_valuta', 'jml_dana'));
				
				
				$data['kurs']			= toNumber($data['kurs']);
				$data['jml_dana']		= toNumber($data['jml_dana']);
				
				
				
				$suffix 	= "/KB/".date_format3(post('tgl_transaksi'), 'MM').'/'.date_format3(post('tgl_transaksi'), 'YYYY');
				$id_dok		= $this->get_data("SELECT (NVL((SELECT MAX(SUBSTR(ID_DOK,0,LENGTH(ID_DOK)-LENGTH('".$suffix."'))) FROM KEU_TRANS_KAS_BANK WHERE SUBSTR(ID_DOK,-1*LENGTH('".$suffix."'),LENGTH('".$suffix."')) = '".$suffix."'),0)+1) CODE FROM DUAL");
				$id_dok		= str_pad($id_dok, 2, '0', STR_PAD_LEFT).$suffix;
				
				$data['id_dok']					= $id_dok;
				$data['id_reff']				= post('id_pinjaman');
				$data['status_transaksi']		= 'Y';
				$data['tgl_transaksi']			= date_format3(post('tgl_transaksi'), 'YYYY-MM-DD');
				$data['keterangan']				.= '(Periode '.post('periode').')';
				$data['create_date']			= NOW;
				$data['create_user']			= USERID;

				$this->PinjamanAnggota->Add($data);
				
				
				$this->SP->SP_GL_PENERIMAAN_PINJAMAN($id_dok,post('id_pinjaman'),post('periode'));

				$ls_response = "Data Tersimpan";
				
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_dok	= get('id_dok');
		
		if ($id_dok != "")
		{
			// Delete Single
			$ls_result = $this->PinjamanAnggota->DeleteSingle($id_dok);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->PinjamanAnggota->DeleteSingle($row['id_dok']);
			}
		}
		
		echo $ls_result;
	}
	
	
	
}
?>